package com.situ.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.situ.context.BaseContext;
import com.situ.domain.dto.AddManagerDTO;
import com.situ.domain.dto.LogInDTO;
import com.situ.domain.dto.ManagerPageQueryDTO;
import com.situ.domain.entity.TManager;
import com.situ.domain.vo.BasePageQueryVO;
import com.situ.domain.vo.ManagerListVO;
import com.situ.ex.NotLoginException;
import com.situ.service.ManagerService;
import com.situ.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "管理员接口")
@RequestMapping("/manage")
@RequiredArgsConstructor
public class ManagerController {

    private final ManagerService managerService;

    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result<String> login(@RequestBody LogInDTO logInDTO) {
        log.info("管理员登录:{}", logInDTO);
        String token = managerService.login(logInDTO);
        return Result.success(200, "登录成功", token);
    }

    /**
     * 检查token状态
     *
     * @param token
     * @return
     */
    @Operation(summary = "检查token状态接口")
    @PostMapping("/checkToken")
    public Result<TManager> checkToken(String token) {
        log.info("检查token:{}", token);
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        TManager manager = null;
        manager = managerService.lambdaQuery()
                .eq(TManager::getId, loginId)
                .eq(TManager::getIsDelete, 0)
                .one();
        if (manager == null) {
            throw new NotLoginException("伪造的token");
        }
        TManager manager1 = TManager
                .builder()
                .id(manager.getId())
                .username(manager.getUsername())
                .avatar(manager.getAvatar())
                .build();
        return Result.success(200, "用户已登录", manager1);
    }


    @GetMapping("/getManagerPageList")
    @Operation(summary = "分页获取管理员列表")
    public Result<BasePageQueryVO<ManagerListVO>> getManagerPageList(@ParameterObject ManagerPageQueryDTO pageQueryDTO) {
        BasePageQueryVO<ManagerListVO> pageQueryVO = managerService.getManagerList(pageQueryDTO);
        return Result.success(200, "获取管理员列表成功", pageQueryVO);
    }


    @PutMapping("/changeIsDelete")
    @Operation(summary = "修改用户状态")
    public Result changeIsDelete(Integer id, Integer isDelete) {
        log.info("修改用户状态:{},{}", id, isDelete);
        managerService
                .lambdaUpdate()
                .eq(TManager::getId, id)
                .set(TManager::getIsDelete, isDelete)
                .set(TManager::getModifiedTime, new Date())
                .set(TManager::getModifiedUser, BaseContext.getCurrentId().toString())
                .update();
        return Result.ok("修改用户状态成功");
    }

    @PostMapping("/addManager")
    @Operation(summary = "添加管理员接口")
    public Result addManager(@RequestBody AddManagerDTO managerDTO) {
        log.info("添加管理员:{}", managerDTO);
        managerService.addManager(managerDTO);
        return Result.ok("添加成功");
    }


    @PostMapping("/logout")
    @Operation(summary = "退出登录接口")
    public Result logout() {
        log.info("退出登录");
        StpUtil.logout();
        return Result.ok("退出登录成功");
    }

}
